package com.my.network.http;

import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

/**
 * @author: marshal
 * @date: 2023-04-12 11:21
 */
public class HttpClientTest {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
        // 创建HTTP 客户端
        HttpClient client = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_1_1)
                .followRedirects(HttpClient.Redirect.NORMAL)
                .connectTimeout(Duration.ofSeconds(20))
                .build();
        // 创建HTTP 请求
        HttpRequest request = HttpRequest.newBuilder(new URI("http://127.0.0.1:9527/get")).build();
        // 发送请求
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        // 打印结果
        System.out.println("status :" + response.statusCode());
        System.out.println("response body:");
        System.out.println(response.body());
    }
}
